package com.hp.bon.sgw.domain;

import java.net.URI;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.google.gson.annotations.Expose;
import com.hp.bon.sgw.Constants.NodeStatus;
import com.hp.bon.sgw.core.channel.IChannel;
import com.hp.bon.sgw.util.Comutils;

/**
 * 
 * @author wuzhih
 * 
 */
@Entity
@Table(name = "T_NODE")
public class Node {

	@Expose
	private int id;
	@Expose
	private int wideNodeId;
	@Expose
	private int channelId;
	@Expose
	private int connectType; // -1:不连接;0:sgw被动连接;1:sgw主动连接;
	/**
	 * 节点的uri dcc aaa://ip:port webservice ws://ip ctp ctp://ip
	 * 
	 */
	@Expose
	private String uri;
	@Expose
	private String hostId;
	@Expose
	private String nodeCode;
	@Expose
	private String nodeName;
	@Expose
	private String nodeDesc;
	@Expose
	private String password;
	@Expose
	private int nodeType;// SR,scsp,ws,dcc
	@Expose
	private int status = NodeStatus.OFF_LINE;// 节点状态
	@Expose
	private Date modifiedTime;
	@Expose
	private Date statusTime;
	@Expose
	private int maxSendQueueSize;

	/**
	 * 2011-12-28 sunhy 扩展针对webservice接入的鉴权 UP:1)UID+PWD ----外部平台IP不唯一时
	 * UPI:2)UID+PWD+IP ----外部平台IP唯一 3)UID+PWD+数字签名 ----当外部平台支持时 （待定）
	 * 4)UID+PWD+数字签名+关键字段加密 ----当外部平台支持时（待定）
	 */
	@Expose
	private String checkType;
	/**
	 * 外部网元作为Server端的地址
	 */
	@Expose
	private String serverUrl;

	/**
	 * 是否代网元激活能力:0:否,1:是
	 */
	@Expose
	private int needActive = 0;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "NODE_TYPE", nullable = false)
	public int getNodeType() {
		return nodeType;
	}

	public void setNodeType(int nodeType) {
		this.nodeType = nodeType;
	}

	@Column(name = "NODE_CODE", nullable = false)
	public String getNodeCode() {
		return nodeCode;
	}

	@Column(name = "MODIFIED_TIME", columnDefinition = "TIMESTAMP(6)")
	public Date getModifiedTime() {
		return modifiedTime;
	}

	public void setModifiedTime(Date updateTime) {
		this.modifiedTime = updateTime;
	}

	@Column(name = "STATUS_TIME", columnDefinition = "TIMESTAMP(6)")
	public Date getStatusTime() {
		return statusTime;
	}

	public void setStatusTime(Date statusTime) {
		this.statusTime = statusTime;
	}

	@Transient
	public int getPort() {
		if (uri == null || uri.length() == 0) {
			return 0;
		}
		URI url;
		try {
			url = new URI(this.uri);
		} catch (Exception e) {
			return 0;
		}
		return url.getPort();
	}

	public void setNodeCode(String code) {
		this.nodeCode = code;
	}

	public int getStatus() {
		return status;
	}

	public void setNewStatus(int status, long time) {
		this.status = status;
		this.statusTime = new Date(time);
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	@Column(name = "HOST_ID", nullable = false,length = 64)
	public String getHostId() {
		return hostId;
	}

	public void setHostId(String hostId) {
		this.hostId = hostId;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@Column(name = "NODE_NAME", nullable = false, length = 50)
	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public int getWideNodeId() {
		return wideNodeId;
	}

	public void setWideNodeId(int nodeId) {
		this.wideNodeId = nodeId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{hostId:").append(hostId).append(",uri:").append(uri).append(",status:").append(status).append(",status time:").append(Comutils.formatTime(this.statusTime, "yyyyMMdd HH:mm:ss.SSS")).append("}");
		return sb.toString();

	}

	@Column(name = "NODE_DESC", nullable = true, length = 128)
	public String getNodeDesc() {
		return nodeDesc;
	}

	public void setNodeDesc(String nodeDesc) {
		this.nodeDesc = nodeDesc;
	}

	@Column(name = "CHECK_TYPE", nullable = true, length = 50)
	public String getCheckType() {
		return checkType;
	}

	public void setCheckType(String checkType) {
		this.checkType = checkType;
	}

	@Column(name = "SERVER_URL", nullable = true, length = 50)
	public String getServerUrl() {
		return serverUrl;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	@Column(name = "CHANNEL_ID")
	public int getChannelId() {
		return channelId;
	}

	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}

	@Column(name = "CONNECT_TYPE")
	public int getConnectType() {
		return connectType;
	}

	public void setConnectType(int connectType) {
		this.connectType = connectType;
	}

	@Column(name = "MAX_SEND_QUEUE_SIZE")
	public int getMaxSendQueueSize() {
		return maxSendQueueSize;
	}

	public void setMaxSendQueueSize(int maxSendQueueSize) {
		this.maxSendQueueSize = maxSendQueueSize;
	}

	@Column(name = "NEED_ACTIVE")
	public int getNeedActive() {
		return needActive;
	}

	public void setNeedActive(int needActive) {
		this.needActive = needActive;
	}

	@Transient
	public String getNECode() {
		// 得到9位的网元编码
		if (nodeCode == null || nodeCode.length() < 10) {
			return nodeCode;
		} else {
			return nodeCode.substring(0, 9);
		}
	}

	@Transient
	public String getIP() {
		if (uri == null || uri.length() == 0) {
			return "";
		}
		URI url;
		try {
			url = new URI(this.uri);
		} catch (Exception e) {
			return "";
		}
		return url.getHost();
	}

	public boolean checkAccessedIP(String requestIP) {
		List<String> myIPs = getIPs();
		if (myIPs.contains(requestIP)) {
			return true;
		} else if (myIPs.contains("127.0.0.1") || myIPs.contains("localhost") || myIPs.contains("0.0.0.0")) {
			return requestIP.equals("127.0.0.1") || requestIP.equalsIgnoreCase("localhost") || requestIP.equals("0.0.0.0");
		} else {
			return false;
		}
	}

	@Transient
	public String getRealm() {
		int i = hostId.indexOf('@');
		return hostId.substring(hostId.indexOf('@') + 1);
	}

	@Transient
	public String[] getUris() {
		if (uri != null && !"".equals(uri.trim())) {
			return uri.split("\\|");
		}
		return null;
	}

	@Transient
	public List<String> getIPs() {
		String[] uris = getUris();
		List<String> ips = new ArrayList<String>();
		if (uris != null && uris.length > 0) {
			for (String _url : uris) {
				URI url;
				try {
					url = new URI(_url);
					ips.add(url.getHost());
				} catch (Exception e) {

				}
			}
		}
		return ips;
	}

	double sendQueuePercent = 0;

	@Transient
	public double getSendQueuePercent() {
		return sendQueuePercent;
	}

	public void setSendQueuePercent(double sendQueuePercent) {
		this.sendQueuePercent = sendQueuePercent;
	}

	@Transient
	public IChannel channel;

	@Transient
	public IChannel getChannel() {
		return channel;
	}

	public void setChannel(IChannel channel) {
		this.channel = channel;
	}

	@Transient
	public String getFirstUri() {
		String[] uris = getUris();
		if (uris != null && uris.length > 0) {
			return uris[0];
		}
		return uri;
	}
}
